package com.leonne.harpacristabrasileira.lists.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.leonne.harpacristabrasileira.R;
import com.leonne.harpacristabrasileira.dao.AnthemsDAO;
import com.leonne.harpacristabrasileira.lists.vo.AnthemVO;
import com.leonne.harpacristabrasileira.tools.DataCache;

public class AnthemsAdapter extends GenericAdapter {
	
	private int tmpPosition;
    
	public AnthemsAdapter(Activity activity, ArrayList<AnthemVO> anthems) {
        super(activity, anthems);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        this.tmpPosition = position;
        if(convertView==null)
            vi = this.getInflater().inflate(R.layout.anthems_list_layout, null);
        AnthemVO anthem = (AnthemVO)this.getObjects().get(position);
        TextView tvNumber = (TextView) vi.findViewById(R.id.itemNumberAnthemsList);
        TextView tvTitle = (TextView) vi.findViewById(R.id.itemTitleAnthemsList);
        TextView tvMainVerse = (TextView) vi.findViewById(R.id.itemMainVerseAnthemsList);
        ImageView iv = (ImageView) vi.findViewById(R.id.itemImageAnthemsList);
        iv.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				TextView tv = (TextView) ((View) v.getParent()).findViewById(R.id.itemNumberAnthemsList);
				ImageView iv = (ImageView) v;
				AnthemsDAO dao = new AnthemsDAO(getActivity().getBaseContext());
				int resource;
				String value;
				if (iv.getTag().equals("1")){
					resource = R.drawable.notfavoritestar;
					value = "0";
				} else {
					resource = R.drawable.favoritestar;
					value = "1";
				}
				String sql = "UPDATE anthems SET isfavorite="+ value +" WHERE idanthem = " + tv.getText().toString();
				dao.execSql(sql);
				
				int index = 0;
				int idAnthem = Integer.parseInt(tv.getText().toString());
				if (DataCache.anthemsCache.size() == 640)
					index = idAnthem - 1;
				else {
					for (int i = 0;i < DataCache.anthemsCache.size();i++){
						AnthemVO tmp = (AnthemVO) DataCache.anthemsCache.get(i);
						if (tmp.getIdAnthem() == idAnthem){
							index = i;
							break;
						}
					}
				}
				AnthemVO anthem = (AnthemVO) DataCache.anthemsCache.get(index);
				anthem.setIsFavorite(Integer.parseInt(value));
				
				iv.setImageResource(resource);
				iv.setTag(value);
			}
		});
        if (anthem.getIsFavorite() == 1){        	
            iv.setImageResource(R.drawable.favoritestar);
            iv.setTag("1");
        } else {
        	iv.setImageResource(R.drawable.notfavoritestar);
            iv.setTag("0");
        }
        tvNumber.setText(String.valueOf(anthem.getIdAnthem()));
        tvTitle.setText(anthem.getTitle());
        tvMainVerse.setText(anthem.getMainVerse());
        
        RelativeLayout rl  = (RelativeLayout)vi.findViewById(R.id.layoutRootAnthemsList);
//        rl.setBackgroundColor(Color.parseColor("#FFFFFF"));
//        if(this.getSelectedPos() == position){
//        	rl.setBackgroundColor(Color.parseColor("#FFE4C4"));
//        }else{
//        	rl.setBackgroundColor(Color.parseColor("#E5E5E5"));
//        }
        return vi;
    }
}